

<!DOCTYPE html>
<html lang="zh-CN" data-default-color-scheme=&#34;light&#34;>



<head>
  <meta charset="UTF-8">
  <link rel="apple-touch-icon" sizes="76x76" href="/img/me/title.jpg">
  <link rel="icon" type="image/png" href="/img/me/title.jpg">
  <meta name="viewport"
        content="width=device-width, initial-scale=1.0, maximum-scale=1.0, user-scalable=no, shrink-to-fit=no">
  <meta http-equiv="x-ua-compatible" content="ie=edge">
  
  <meta name="theme-color" content="#2f4154">
  <meta name="description" content="你要悄悄学web，然后惊艳所有人">
  <meta name="author" content="yxfan">
  <meta name="keywords" content="">
  <title>tapable实现 - yxfan</title>

  <link  rel="stylesheet" href="https://fastly.jsdelivr.net/npm/bootstrap@4.5.3/dist/css/bootstrap.min.css" />


  <link  rel="stylesheet" href="https://fastly.jsdelivr.net/npm/github-markdown-css@4.0.0/github-markdown.min.css" />
  <link  rel="stylesheet" href="/lib/hint/hint.min.css" />

  
    
    
      
      <link  rel="stylesheet" href="https://fastly.jsdelivr.net/npm/highlight.js@10.4.0/styles/github-gist.min.css" />
    
  

  
    <link  rel="stylesheet" href="https://fastly.jsdelivr.net/npm/@fancyapps/fancybox@3.5.7/dist/jquery.fancybox.min.css" />
  



<!-- 主题依赖的图标库，不要自行修改 -->

<link rel="stylesheet" href="//at.alicdn.com/t/font_1749284_ba1fz6golrf.css">



<link rel="stylesheet" href="//at.alicdn.com/t/font_1736178_kmeydafke9r.css">


<link  rel="stylesheet" href="/css/main.css" />

<!-- 自定义样式保持在最底部 -->


  <script id="fluid-configs">
    var Fluid = window.Fluid || {};
    var CONFIG = {"hostname":"yoursite.com","root":"/","version":"1.8.7","typing":{"enable":true,"typeSpeed":70,"cursorChar":"_","loop":false},"anchorjs":{"enable":true,"element":"h1,h2,h3","placement":"right","visible":"always","Option":"§ |","icon":"#"},"progressbar":{"enable":true,"height_px":3,"color":"#29d","options":{"showSpinner":false,"trickleSpeed":100}},"copy_btn":true,"image_zoom":{"enable":true},"toc":{"enable":true,"headingSelector":"h1,h2,h3,h4,h5,h6","collapseDepth":20},"lazyload":{"enable":true,"onlypost":false},"web_analytics":{"enable":true,"baidu":null,"google":null,"gtag":null,"tencent":{"sid":null,"cid":null},"woyaola":null,"cnzz":null,"leancloud":{"app_id":"AGrJP3tUCktoWpoiRigqj3zy-gzGzoHsz","app_key":"r0u61FlVNs3yvXwGYocWG3qm","server_url":null}}};
  </script>
  <script  src="/js/utils.js" ></script>
  <script  src="/js/color-schema.js" ></script>
<meta name="generator" content="Hexo 5.4.0"></head>


<body>
  <header style="height: 70vh;">
    <nav id="navbar" class="navbar fixed-top  navbar-expand-lg navbar-dark scrolling-navbar">
  <div class="container">
    <a class="navbar-brand"
       href="/">&nbsp;<strong>yxfan</strong>&nbsp;</a>

    <button id="navbar-toggler-btn" class="navbar-toggler" type="button" data-toggle="collapse"
            data-target="#navbarSupportedContent"
            aria-controls="navbarSupportedContent" aria-expanded="false" aria-label="Toggle navigation">
      <div class="animated-icon"><span></span><span></span><span></span></div>
    </button>

    <!-- Collapsible content -->
    <div class="collapse navbar-collapse" id="navbarSupportedContent">
      <ul class="navbar-nav ml-auto text-center">
        
          
          
          
          
            <li class="nav-item">
              <a class="nav-link" href="/">
                <i class="iconfont icon-home-fill"></i>
                首页
              </a>
            </li>
          
        
          
          
          
          
            <li class="nav-item">
              <a class="nav-link" href="/archives/">
                <i class="iconfont icon-archive-fill"></i>
                归档
              </a>
            </li>
          
        
          
          
          
          
            <li class="nav-item">
              <a class="nav-link" href="/tags/">
                <i class="iconfont icon-tags-fill"></i>
                标签
              </a>
            </li>
          
        
          
          
          
          
            <li class="nav-item">
              <a class="nav-link" href="/about/">
                <i class="iconfont icon-user-fill"></i>
                关于
              </a>
            </li>
          
        
          
          
          
          
            <li class="nav-item">
              <a class="nav-link" href="/links/">
                <i class="iconfont icon-link-fill"></i>
                友链
              </a>
            </li>
          
        
        
          <li class="nav-item" id="search-btn">
            <a class="nav-link" data-toggle="modal" data-target="#modalSearch">&nbsp;<i
                class="iconfont icon-search"></i>&nbsp;</a>
          </li>
        
        
          <li class="nav-item" id="color-toggle-btn">
            <a class="nav-link" href="javascript:">&nbsp;<i
                class="iconfont icon-dark" id="color-toggle-icon"></i>&nbsp;</a>
          </li>
        
      </ul>
    </div>
  </div>
</nav>

    <div class="banner" id="banner" parallax=true
         style="background: url('/img/background/lu_bule.png') no-repeat center center;
           background-size: cover;">
      <div class="full-bg-img">
        <div class="mask flex-center" style="background-color: rgba(0, 0, 0, 0.3)">
          <div class="page-header text-center fade-in-up">
            <span class="h2" id="subtitle" title="tapable实现">
              
            </span>

            
              <div class="mt-3">
  
  
    <span class="post-meta">
      <i class="iconfont icon-date-fill" aria-hidden="true"></i>
      <time datetime="2021-11-22 14:05" pubdate>
        2021年11月22日 下午
      </time>
    </span>
  
</div>

<div class="mt-1">
  
    
    <span class="post-meta mr-2">
      <i class="iconfont icon-chart"></i>
      2.3k 字
    </span>
  

  
    
    <span class="post-meta mr-2">
      <i class="iconfont icon-clock-fill"></i>
      
      
      38
       分钟
    </span>
  

  
  
    
      <!-- 不蒜子统计文章PV -->
      <span id="busuanzi_container_page_pv" style="display: none">
        <i class="iconfont icon-eye" aria-hidden="true"></i>
        <span id="busuanzi_value_page_pv"></span> 次
      </span>
    
  
</div>

            
          </div>

          
        </div>
      </div>
    </div>
  </header>

  <main>
    
      

<div class="container-fluid nopadding-x">
  <div class="row nomargin-x">
    <div class="d-none d-lg-block col-lg-2"></div>
    <div class="col-lg-8 nopadding-x-md">
      <div class="container nopadding-x-md" id="board-ctn">
        <div class="py-5" id="board">
          <article class="post-content mx-auto">
            <!-- SEO header -->
            <h1 style="display: none">tapable实现</h1>
            
            <div class="markdown-body">
              <p>tapable有以下9种钩子</p>
<ul>
<li><p>SyncHook</p>
</li>
<li><p>SyncBailHook</p>
</li>
<li><p>SyncLoopHook</p>
</li>
<li><p>SyncWaterfallHook</p>
</li>
<li><p>AsyncParallelHook</p>
</li>
<li><p>AsyncParallelBailHook</p>
</li>
<li><p>AsyncSeriesHook</p>
</li>
<li><p>AsyncSeriesBailHook</p>
</li>
<li><p>AsyncSeriesWaterfallHook</p>
</li>
</ul>
<h3 id="SyncHook"><a href="#SyncHook" class="headerlink" title="SyncHook"></a>SyncHook</h3><h4 id="用法："><a href="#用法：" class="headerlink" title="用法："></a>用法：</h4><div class="code-wrapper"><pre><code class="hljs javascript"><span class="hljs-keyword">const</span> &#123; SyncHook &#125; = <span class="hljs-built_in">require</span>(<span class="hljs-string">&#x27;tapable&#x27;</span>);
<span class="hljs-keyword">const</span> hook = <span class="hljs-keyword">new</span> SyncHook([<span class="hljs-string">&#x27;arg1&#x27;</span>, <span class="hljs-string">&#x27;arg2&#x27;</span>, <span class="hljs-string">&#x27;arg3&#x27;</span>]);
<span class="hljs-comment">// 注册两个监听函数，tap方法的第一个参数没有实际意义，只是用来做标识</span>
hook.tap(<span class="hljs-string">&#x27;yxfan&#x27;</span>, <span class="hljs-function">(<span class="hljs-params">say</span>) =&gt;</span> &#123;
    <span class="hljs-built_in">console</span>.log(say, <span class="hljs-string">&#x27;: yxfan&#x27;</span>);
&#125;);
hook.tap(<span class="hljs-string">&#x27;cheney&#x27;</span>, <span class="hljs-function">(<span class="hljs-params">say</span>) =&gt;</span> &#123;
    <span class="hljs-built_in">console</span>.log(say, <span class="hljs-string">&#x27; : cheney&#x27;</span>)
&#125;);
<span class="hljs-comment">// 触发</span>
hook.call(<span class="hljs-string">&#x27;hello&#x27;</span>)
<span class="hljs-comment">// 打印</span>
<span class="hljs-comment">// hello : yxfan</span>
<span class="hljs-comment">// hello : cheney</span></code></pre></div>

<h4 id="实现："><a href="#实现：" class="headerlink" title="实现："></a>实现：</h4><div class="code-wrapper"><pre><code class="hljs javascript"><span class="hljs-class"><span class="hljs-keyword">class</span> <span class="hljs-title">SyncHook</span> </span>&#123;
    <span class="hljs-function"><span class="hljs-title">constructor</span>(<span class="hljs-params">args</span>)</span> &#123; <span class="hljs-comment">// </span>
        <span class="hljs-built_in">this</span>.tasks = [];
    &#125;
    <span class="hljs-comment">// 订阅</span>
    <span class="hljs-function"><span class="hljs-title">tap</span>(<span class="hljs-params">name, task</span>)</span> &#123;
        <span class="hljs-built_in">this</span>.tasks.push(task);
    &#125;
    <span class="hljs-comment">// 发布</span>
    <span class="hljs-function"><span class="hljs-title">call</span>(<span class="hljs-params">...args</span>)</span> &#123;
        <span class="hljs-built_in">this</span>.tasks.forEach(<span class="hljs-function"><span class="hljs-params">task</span> =&gt;</span> &#123;
            task(...args);
        &#125;);
    &#125;
&#125;
<span class="hljs-built_in">module</span>.exports = SyncHook;</code></pre></div>

<h3 id="SyncBailHook"><a href="#SyncBailHook" class="headerlink" title="SyncBailHook"></a>SyncBailHook</h3><p>SyncBailHook同步熔断保险钩子,即return一个非undefined的值，则不再继续执行后面的监听函数</p>
<h4 id="用法：-1"><a href="#用法：-1" class="headerlink" title="用法："></a>用法：</h4><div class="code-wrapper"><pre><code class="hljs javascript"><span class="hljs-keyword">const</span> &#123; SyncBailHook &#125; = <span class="hljs-built_in">require</span>(<span class="hljs-string">&#x27;tapable&#x27;</span>);
<span class="hljs-class"><span class="hljs-keyword">class</span> <span class="hljs-title">Test</span> </span>&#123;
    hooks = &#123;
        <span class="hljs-attr">arch</span>: <span class="hljs-keyword">new</span> SyncBailHook([<span class="hljs-string">&#x27;arg1&#x27;</span>])
    &#125;
&#125;
<span class="hljs-keyword">const</span> test = <span class="hljs-keyword">new</span> Test();
test.hooks.arch.tap(<span class="hljs-string">&#x27;yxfan&#x27;</span>, <span class="hljs-function">(<span class="hljs-params">say</span>) =&gt;</span> &#123;
    <span class="hljs-built_in">console</span>.log(say, <span class="hljs-string">&#x27;: yxfan&#x27;</span>);
    <span class="hljs-keyword">return</span> <span class="hljs-string">&#x27;yxfan 今天调休啦&#x27;</span>; <span class="hljs-comment">// 不会继续执行下面的事件</span>
    <span class="hljs-comment">// return undefined 则继续往下执行</span>
&#125;)
test.hooks.arch.tap(<span class="hljs-string">&#x27;cheney&#x27;</span>, <span class="hljs-function">(<span class="hljs-params">say</span>) =&gt;</span> &#123;
    <span class="hljs-built_in">console</span>.log(say, <span class="hljs-string">&#x27;: cheney&#x27;</span>);
&#125;)
test.hooks.arch.call(<span class="hljs-string">&#x27;hello&#x27;</span>)
<span class="hljs-comment">// 打印：hello : yxfan</span></code></pre></div>

<h4 id="实现：-1"><a href="#实现：-1" class="headerlink" title="实现："></a>实现：</h4><div class="code-wrapper"><pre><code class="hljs javascript"><span class="hljs-class"><span class="hljs-keyword">class</span> <span class="hljs-title">SyncBailHook</span> </span>&#123;
    <span class="hljs-function"><span class="hljs-title">constructor</span>(<span class="hljs-params">args</span>)</span> &#123;
        <span class="hljs-built_in">this</span>.tasks = [];
    &#125;
    <span class="hljs-comment">// 订阅</span>
    <span class="hljs-function"><span class="hljs-title">tap</span>(<span class="hljs-params">name, task</span>)</span> &#123;
        <span class="hljs-built_in">this</span>.tasks.push(task);
    &#125;
    <span class="hljs-comment">// 发布</span>
    <span class="hljs-function"><span class="hljs-title">call</span>(<span class="hljs-params">...args</span>)</span> &#123;
        <span class="hljs-keyword">for</span>(<span class="hljs-keyword">let</span> i = <span class="hljs-number">0</span>; i &lt; <span class="hljs-built_in">this</span>.tasks.length; i++) &#123;
            <span class="hljs-keyword">const</span> result = <span class="hljs-built_in">this</span>.tasks[i](...args);
            <span class="hljs-keyword">if</span> (result) <span class="hljs-keyword">return</span>
        &#125;
    &#125;
&#125;
<span class="hljs-built_in">module</span>.exports = SyncBailHook;</code></pre></div>

<h3 id="SyncWaterfallHook"><a href="#SyncWaterfallHook" class="headerlink" title="SyncWaterfallHook"></a>SyncWaterfallHook</h3><p>SyncWaterfallHook 瀑布钩子,上一个监听函数的返回值，作为下一个监听函数的参数，像瀑布一样传递参数</p>
<h4 id="用法：-2"><a href="#用法：-2" class="headerlink" title="用法："></a>用法：</h4><div class="code-wrapper"><pre><code class="hljs javascript"><span class="hljs-keyword">const</span> &#123; SyncWaterfallHook &#125; = <span class="hljs-built_in">require</span>(<span class="hljs-string">&#x27;tapable&#x27;</span>);
<span class="hljs-class"><span class="hljs-keyword">class</span> <span class="hljs-title">Test</span> </span>&#123;
    hooks = &#123;
        <span class="hljs-attr">arch</span>: <span class="hljs-keyword">new</span> SyncWaterfallHook([<span class="hljs-string">&#x27;arg1&#x27;</span>])
    &#125;
&#125;
<span class="hljs-keyword">const</span> test = <span class="hljs-keyword">new</span> Test();
test.hooks.arch.tap(<span class="hljs-string">&#x27;yxfan&#x27;</span>, <span class="hljs-function">(<span class="hljs-params">say</span>) =&gt;</span> &#123;
    <span class="hljs-built_in">console</span>.log(say, <span class="hljs-string">&#x27;: yxfan&#x27;</span>);
    <span class="hljs-keyword">return</span> <span class="hljs-string">&#x27;yxfan ok&#x27;</span>
&#125;)
test.hooks.arch.tap(<span class="hljs-string">&#x27;cheney&#x27;</span>, <span class="hljs-function">(<span class="hljs-params">data</span>) =&gt;</span> &#123;
    <span class="hljs-built_in">console</span>.log(data);
    <span class="hljs-keyword">return</span> <span class="hljs-string">&#x27;cheney ok&#x27;</span>

&#125;)
test.hooks.arch.tap(<span class="hljs-string">&#x27;xiaopa&#x27;</span>, <span class="hljs-function">(<span class="hljs-params">data</span>) =&gt;</span> &#123;
    <span class="hljs-built_in">console</span>.log(data);
&#125;)
test.hooks.arch.call(<span class="hljs-string">&#x27;hello&#x27;</span>)
<span class="hljs-comment">// 打印如下：</span>
<span class="hljs-comment">// hello : yxfan</span>
<span class="hljs-comment">// yxfan ok</span>
<span class="hljs-comment">// cheney ok</span></code></pre></div>

<h4 id="实现：-2"><a href="#实现：-2" class="headerlink" title="实现："></a>实现：</h4><div class="code-wrapper"><pre><code class="hljs javascript"><span class="hljs-class"><span class="hljs-keyword">class</span> <span class="hljs-title">SyncWaterfallHook</span> </span>&#123;
    <span class="hljs-function"><span class="hljs-title">constructor</span>(<span class="hljs-params">...args</span>)</span> &#123;
        <span class="hljs-built_in">this</span>.tasks = [];
    &#125;
    <span class="hljs-function"><span class="hljs-title">tap</span>(<span class="hljs-params">name, task</span>)</span> &#123;
        <span class="hljs-built_in">this</span>.tasks.push(task);
    &#125;
    <span class="hljs-function"><span class="hljs-title">call</span>(<span class="hljs-params">...args</span>)</span> &#123;
        <span class="hljs-keyword">const</span> [first, ...other] = <span class="hljs-built_in">this</span>.tasks;
        <span class="hljs-keyword">let</span> result = first(...args);
        other.reduce(<span class="hljs-function">(<span class="hljs-params">pre, next</span>) =&gt;</span> &#123;
            <span class="hljs-keyword">let</span> ret = next(pre);
            <span class="hljs-keyword">if</span> (ret) <span class="hljs-keyword">return</span> ret;
            <span class="hljs-keyword">return</span> result;
        &#125;, result);
    &#125;
&#125;
<span class="hljs-built_in">module</span>.exports = SyncWaterfallHook;</code></pre></div>

<h3 id="SyncLoopHook"><a href="#SyncLoopHook" class="headerlink" title="SyncLoopHook"></a>SyncLoopHook</h3><p>SyncLoopHook 只要监听函数不返回undefined，就循环执行该监听函数</p>
<div class="code-wrapper"><pre><code class="hljs javascript"><span class="hljs-keyword">const</span> &#123; SyncLoopHook &#125; = <span class="hljs-built_in">require</span>(<span class="hljs-string">&#x27;tapable&#x27;</span>);
<span class="hljs-class"><span class="hljs-keyword">class</span> <span class="hljs-title">Test</span> </span>&#123;
    hooks = &#123;
        <span class="hljs-attr">arch</span>: <span class="hljs-keyword">new</span> SyncLoopHook([<span class="hljs-string">&#x27;arg1&#x27;</span>, <span class="hljs-string">&#x27;arg2&#x27;</span>])
    &#125;
&#125;
<span class="hljs-keyword">let</span> index = <span class="hljs-number">0</span>;
<span class="hljs-keyword">const</span> test = <span class="hljs-keyword">new</span> Test();
test.hooks.arch.tap(<span class="hljs-string">&#x27;yxfan&#x27;</span>, <span class="hljs-function">(<span class="hljs-params">say</span>) =&gt;</span> &#123;
    <span class="hljs-built_in">console</span>.log(say, <span class="hljs-string">&#x27;: yxfan&#x27;</span>, index);    
    <span class="hljs-keyword">return</span> ++index === <span class="hljs-number">3</span> ? <span class="hljs-literal">undefined</span> : <span class="hljs-string">&#x27;wait a moment&#x27;</span>
&#125;)
test.hooks.arch.tap(<span class="hljs-string">&#x27;cheney&#x27;</span>, <span class="hljs-function">(<span class="hljs-params">say</span>) =&gt;</span> &#123;
    <span class="hljs-built_in">console</span>.log(say, <span class="hljs-string">&#x27;: cheney&#x27;</span>);
&#125;)
test.hooks.arch.call(<span class="hljs-string">&#x27;hello&#x27;</span>)
<span class="hljs-comment">// 打印如下：</span>
<span class="hljs-comment">// hello : yxfan 0</span>
<span class="hljs-comment">// hello : yxfan 1</span>
<span class="hljs-comment">// hello : yxfan 2</span>
<span class="hljs-comment">// hello : cheney</span></code></pre></div>

<h4 id="实现：-3"><a href="#实现：-3" class="headerlink" title="实现："></a>实现：</h4><div class="code-wrapper"><pre><code class="hljs javascript"><span class="hljs-class"><span class="hljs-keyword">class</span> <span class="hljs-title">SyncLoopHook</span> </span>&#123;
    <span class="hljs-function"><span class="hljs-title">constructor</span>(<span class="hljs-params"></span>)</span> &#123;
        <span class="hljs-built_in">this</span>.tasks = [];
    &#125;
    <span class="hljs-function"><span class="hljs-title">tap</span>(<span class="hljs-params">name, task</span>)</span> &#123;
        <span class="hljs-built_in">this</span>.tasks.push(task);
    &#125;
    <span class="hljs-function"><span class="hljs-title">call</span>(<span class="hljs-params">...args</span>)</span> &#123;
        <span class="hljs-built_in">this</span>.tasks.forEach(<span class="hljs-function"><span class="hljs-params">task</span> =&gt;</span> &#123;
            <span class="hljs-keyword">let</span> result;
            <span class="hljs-keyword">do</span> &#123;
                result = task(...args)
            &#125; <span class="hljs-keyword">while</span>(result != <span class="hljs-literal">undefined</span>)
        &#125;)
    &#125;
&#125;
<span class="hljs-built_in">module</span>.exports = SyncLoopHook;</code></pre></div>

<h3 id="AsyncParallelHook"><a href="#AsyncParallelHook" class="headerlink" title="AsyncParallelHook"></a>AsyncParallelHook</h3><p>AsyncParallelHook 异步并行钩子</p>
<p>tapable库中，有三种事件注册方式： </p>
<ul>
<li>tap -同步注册  </li>
<li>tapAsync-异步注册，接受第二个参数cb ，cb方法调用表示改事件执行完了 </li>
<li>tapPromise-接受一个参数Promise</li>
</ul>
<p>有三种事件发布方式：</p>
<ul>
<li>call</li>
<li>callAsync</li>
<li>promise</li>
</ul>
<div class="code-wrapper"><pre><code class="hljs javascript"><span class="hljs-comment">/**</span>
<span class="hljs-comment"> * tapable库中，有三种注册方式 tap -同步注册  tapAsync-异步注册，接受第二个参数cb  tapPromise-一个参数Promise</span>
<span class="hljs-comment"> */</span>
<span class="hljs-keyword">const</span> &#123; AsyncParallelHook &#125; = <span class="hljs-built_in">require</span>(<span class="hljs-string">&#x27;tapable&#x27;</span>);
<span class="hljs-class"><span class="hljs-keyword">class</span> <span class="hljs-title">Hook</span> </span>&#123;
    <span class="hljs-function"><span class="hljs-title">constructor</span>(<span class="hljs-params"></span>)</span> &#123;
        <span class="hljs-built_in">this</span>.hooks = &#123;
            <span class="hljs-attr">arch</span>: <span class="hljs-keyword">new</span> AsyncParallelHook([<span class="hljs-string">&#x27;arg1&#x27;</span>])
        &#125;
    &#125;
    <span class="hljs-comment">// 注册监听函数</span>
    <span class="hljs-function"><span class="hljs-title">subscribe</span>(<span class="hljs-params"></span>)</span> &#123;
        <span class="hljs-built_in">this</span>.hooks.arch.tapAsync(<span class="hljs-string">&#x27;primary&#x27;</span>, <span class="hljs-function">(<span class="hljs-params">name, cb</span>) =&gt;</span> &#123;
            <span class="hljs-built_in">setTimeout</span>(<span class="hljs-function">() =&gt;</span> &#123;
                <span class="hljs-built_in">console</span>.log(<span class="hljs-string">&#x27;初级前端：&#x27;</span>, name);
                cb()
            &#125;, <span class="hljs-number">1000</span>)
        &#125;)
        <span class="hljs-built_in">this</span>.hooks.arch.tapAsync(<span class="hljs-string">&#x27;intermediate&#x27;</span>, <span class="hljs-function">(<span class="hljs-params">name, cb</span>) =&gt;</span> &#123;
            <span class="hljs-built_in">setTimeout</span>(<span class="hljs-function">() =&gt;</span> &#123;
                <span class="hljs-built_in">console</span>.log(<span class="hljs-string">&#x27;中级前端：&#x27;</span>, name);
                cb()
            &#125;, <span class="hljs-number">1000</span>)
        &#125;)
        <span class="hljs-built_in">this</span>.hooks.arch.tapAsync(<span class="hljs-string">&#x27;senior&#x27;</span>, <span class="hljs-function">(<span class="hljs-params">name, cb</span>) =&gt;</span> &#123;
            <span class="hljs-built_in">console</span>.log(<span class="hljs-string">&#x27;高级前端：&#x27;</span>, name);
            cb()
        &#125;)
    &#125;
    <span class="hljs-comment">// 执行监听函数</span>
    <span class="hljs-function"><span class="hljs-title">publish</span>(<span class="hljs-params"></span>)</span> &#123;
        <span class="hljs-built_in">this</span>.hooks.arch.callAsync(<span class="hljs-string">&#x27;yxfan&#x27;</span>, <span class="hljs-function">() =&gt;</span> &#123;
            <span class="hljs-built_in">console</span>.log(<span class="hljs-string">&#x27;封神啦！&#x27;</span>);
        &#125;)
    &#125;
&#125;

<span class="hljs-keyword">const</span> hook = <span class="hljs-keyword">new</span> Hook;
hook.subscribe();
hook.publish();</code></pre></div>

<p>因为是异步并行钩子，所以一秒后一次性打印：“初级前端： yxfan”，“中级前端： yxfan”，“高级前端： yxfan”，异步都执行完了，最后才打印“封神啦！”</p>
<p>用tapPromise比较简单，注意用<code>tapPromise</code>注册监听函数，触发时要用<code>promise</code></p>
<div class="code-wrapper"><pre><code class="hljs javascript"><span class="hljs-class"><span class="hljs-keyword">class</span> <span class="hljs-title">HookPromise</span> </span>&#123;
    <span class="hljs-function"><span class="hljs-title">constructor</span>(<span class="hljs-params"></span>)</span> &#123;
        <span class="hljs-built_in">this</span>.hooks = &#123;
            <span class="hljs-attr">arch</span>: <span class="hljs-keyword">new</span> AsyncParallelHook([<span class="hljs-string">&#x27;arg1&#x27;</span>])
        &#125;
    &#125;
    <span class="hljs-comment">// 注册监听函数</span>
    <span class="hljs-function"><span class="hljs-title">subscribe</span>(<span class="hljs-params"></span>)</span> &#123;
        <span class="hljs-built_in">this</span>.hooks.arch.tapPromise(<span class="hljs-string">&#x27;primary&#x27;</span>, <span class="hljs-function">(<span class="hljs-params">name</span>) =&gt;</span> &#123;
            <span class="hljs-keyword">return</span> <span class="hljs-keyword">new</span> <span class="hljs-built_in">Promise</span>(<span class="hljs-function"><span class="hljs-params">resolve</span> =&gt;</span> &#123;
                <span class="hljs-built_in">setTimeout</span>(<span class="hljs-function">() =&gt;</span> &#123;
                    <span class="hljs-built_in">console</span>.log(<span class="hljs-string">&#x27;初级前端：&#x27;</span>, name);
                    resolve()
                &#125;, <span class="hljs-number">1000</span>)
            &#125;)
            
        &#125;)
        <span class="hljs-built_in">this</span>.hooks.arch.tapPromise(<span class="hljs-string">&#x27;intermediate&#x27;</span>, <span class="hljs-function">(<span class="hljs-params">name</span>) =&gt;</span> &#123;
            <span class="hljs-keyword">return</span> <span class="hljs-keyword">new</span> <span class="hljs-built_in">Promise</span>(<span class="hljs-function"><span class="hljs-params">resolve</span> =&gt;</span> &#123;
                <span class="hljs-built_in">setTimeout</span>(<span class="hljs-function">() =&gt;</span> &#123;
                    <span class="hljs-built_in">console</span>.log(<span class="hljs-string">&#x27;中级前端：&#x27;</span>, name);
                    resolve()
                &#125;, <span class="hljs-number">1000</span>)
            &#125;)
        &#125;)
        <span class="hljs-built_in">this</span>.hooks.arch.tapPromise(<span class="hljs-string">&#x27;senior&#x27;</span>, <span class="hljs-function">(<span class="hljs-params">name</span>) =&gt;</span> &#123;
            <span class="hljs-keyword">return</span> <span class="hljs-keyword">new</span> <span class="hljs-built_in">Promise</span>(<span class="hljs-function"><span class="hljs-params">resolve</span> =&gt;</span> &#123;
                <span class="hljs-built_in">setTimeout</span>(<span class="hljs-function">() =&gt;</span> &#123;
                    <span class="hljs-built_in">console</span>.log(<span class="hljs-string">&#x27;高级前端：&#x27;</span>, name);
                    resolve()
                &#125;, <span class="hljs-number">1000</span>)
            &#125;)
        &#125;)
    &#125;
    <span class="hljs-comment">// 执行监听函数</span>
    <span class="hljs-function"><span class="hljs-title">publish</span>(<span class="hljs-params"></span>)</span> &#123;
        <span class="hljs-built_in">this</span>.hooks.arch.promise(<span class="hljs-string">&#x27;yxfan&#x27;</span>).then(<span class="hljs-function">() =&gt;</span> &#123;
            <span class="hljs-built_in">console</span>.log(<span class="hljs-string">&#x27;封神啦！&#x27;</span>);
        &#125;)
    &#125;
&#125;
<span class="hljs-keyword">const</span> hook = <span class="hljs-keyword">new</span> HookPromise();
hook.subscribe();
hook.publish();</code></pre></div>

<h4 id="实现：-4"><a href="#实现：-4" class="headerlink" title="实现："></a>实现：</h4><div class="code-wrapper"><pre><code class="hljs javascript"><span class="hljs-class"><span class="hljs-keyword">class</span> <span class="hljs-title">AsyncParallelHook</span> </span>&#123;
    <span class="hljs-function"><span class="hljs-title">constructor</span>(<span class="hljs-params"></span>)</span> &#123;
        <span class="hljs-built_in">this</span>.tasks = [];
    &#125;
    <span class="hljs-function"><span class="hljs-title">tapAsync</span>(<span class="hljs-params">name, task</span>)</span> &#123;
        <span class="hljs-built_in">this</span>.tasks.push(task);
    &#125;
    <span class="hljs-function"><span class="hljs-title">callAsync</span>(<span class="hljs-params">...args</span>)</span> &#123;
        <span class="hljs-keyword">const</span> endCb = args.pop();
        <span class="hljs-keyword">let</span> index = <span class="hljs-number">0</span>;
        <span class="hljs-keyword">const</span> done = <span class="hljs-function">() =&gt;</span> &#123;
            index++;
            <span class="hljs-keyword">if</span> (index === <span class="hljs-built_in">this</span>.tasks.length) &#123;
                endCb();
            &#125;
        &#125;
        <span class="hljs-built_in">this</span>.tasks.forEach(<span class="hljs-function"><span class="hljs-params">task</span> =&gt;</span> &#123;
            task(...args, done)
        &#125;);
    &#125;
    <span class="hljs-function"><span class="hljs-title">tapPromise</span>(<span class="hljs-params">name, task</span>)</span> &#123;
        <span class="hljs-built_in">this</span>.tasks.push(task);
    &#125;
    <span class="hljs-function"><span class="hljs-title">promise</span>(<span class="hljs-params">...args</span>)</span> &#123;
        <span class="hljs-keyword">let</span> promiseTasks = <span class="hljs-built_in">this</span>.tasks.map(<span class="hljs-function"><span class="hljs-params">task</span> =&gt;</span> task(...args));
        <span class="hljs-keyword">return</span> <span class="hljs-built_in">Promise</span>.all(promiseTasks);
    &#125;
&#125;
<span class="hljs-built_in">module</span>.exports = AsyncParallelHook;</code></pre></div>

<h3 id="AsyncSeriesHook"><a href="#AsyncSeriesHook" class="headerlink" title="AsyncSeriesHook"></a>AsyncSeriesHook</h3><p>AsyncSeriesHook 异步串行钩子</p>
<div class="code-wrapper"><pre><code class="hljs javascript"><span class="hljs-keyword">const</span> &#123; AsyncSeriesHook &#125; = <span class="hljs-built_in">require</span>(<span class="hljs-string">&#x27;tapable&#x27;</span>);
<span class="hljs-class"><span class="hljs-keyword">class</span> <span class="hljs-title">Hook</span> </span>&#123;
    <span class="hljs-function"><span class="hljs-title">constructor</span>(<span class="hljs-params"></span>)</span> &#123;
        <span class="hljs-built_in">this</span>.hooks = &#123;
            <span class="hljs-attr">arch</span>: <span class="hljs-keyword">new</span> AsyncSeriesHook([<span class="hljs-string">&#x27;arg1&#x27;</span>])
        &#125;
    &#125;
    <span class="hljs-comment">// 注册监听函数</span>
    <span class="hljs-function"><span class="hljs-title">subscribe</span>(<span class="hljs-params"></span>)</span> &#123;
        <span class="hljs-built_in">this</span>.hooks.arch.tapAsync(<span class="hljs-string">&#x27;primary&#x27;</span>, <span class="hljs-function">(<span class="hljs-params">name, cb</span>) =&gt;</span> &#123;
            <span class="hljs-built_in">setTimeout</span>(<span class="hljs-function">() =&gt;</span> &#123;
                <span class="hljs-built_in">console</span>.log(<span class="hljs-string">&#x27;初级前端：&#x27;</span>, name);
                cb()
            &#125;, <span class="hljs-number">1000</span>)
        &#125;)
        <span class="hljs-built_in">this</span>.hooks.arch.tapAsync(<span class="hljs-string">&#x27;intermediate&#x27;</span>, <span class="hljs-function">(<span class="hljs-params">name, cb</span>) =&gt;</span> &#123;
            <span class="hljs-built_in">setTimeout</span>(<span class="hljs-function">() =&gt;</span> &#123;
                <span class="hljs-built_in">console</span>.log(<span class="hljs-string">&#x27;中级前端：&#x27;</span>, name);
                cb()
            &#125;, <span class="hljs-number">1000</span>)
        &#125;)
        <span class="hljs-built_in">this</span>.hooks.arch.tapAsync(<span class="hljs-string">&#x27;senior&#x27;</span>, <span class="hljs-function">(<span class="hljs-params">name, cb</span>) =&gt;</span> &#123;
            <span class="hljs-built_in">setTimeout</span>(<span class="hljs-function">() =&gt;</span> &#123;
                <span class="hljs-built_in">console</span>.log(<span class="hljs-string">&#x27;高级前端：&#x27;</span>, name);
                cb()
            &#125;, <span class="hljs-number">1000</span>)
        &#125;)
    &#125;
    <span class="hljs-comment">// 执行监听函数</span>
    <span class="hljs-function"><span class="hljs-title">publish</span>(<span class="hljs-params"></span>)</span> &#123;
        <span class="hljs-built_in">this</span>.hooks.arch.callAsync(<span class="hljs-string">&#x27;yxfan&#x27;</span>, <span class="hljs-function">() =&gt;</span> &#123;
            <span class="hljs-built_in">console</span>.log(<span class="hljs-string">&#x27;封神啦！&#x27;</span>);
        &#125;)
    &#125;
&#125;
<span class="hljs-keyword">const</span> hook = <span class="hljs-keyword">new</span> Hook();
hook.subscribe();
hook.publish();</code></pre></div>

<p>因为是串行钩子，所以依次间隔1秒打印：“初级前端： yxfan”，“中级前端： yxfan”，“高级前端： yxfan”，异步都执行完了，最后才打印“封神啦！”</p>
<p>同理用tapPromise的情况</p>
<div class="code-wrapper"><pre><code class="hljs javascript"><span class="hljs-class"><span class="hljs-keyword">class</span> <span class="hljs-title">HookPromise</span> </span>&#123;
    <span class="hljs-function"><span class="hljs-title">constructor</span>(<span class="hljs-params"></span>)</span> &#123;
        <span class="hljs-built_in">this</span>.hooks = &#123;
            <span class="hljs-attr">arch</span>: <span class="hljs-keyword">new</span> AsyncSeriesHook([<span class="hljs-string">&#x27;arg1&#x27;</span>])
        &#125;
    &#125;
    <span class="hljs-comment">// 注册监听函数</span>
    <span class="hljs-function"><span class="hljs-title">subscribe</span>(<span class="hljs-params"></span>)</span> &#123;
        <span class="hljs-built_in">this</span>.hooks.arch.tapPromise(<span class="hljs-string">&#x27;primary&#x27;</span>, <span class="hljs-function">(<span class="hljs-params">name</span>) =&gt;</span> &#123;
            <span class="hljs-keyword">return</span> <span class="hljs-keyword">new</span> <span class="hljs-built_in">Promise</span>(<span class="hljs-function">(<span class="hljs-params">resolve</span>) =&gt;</span> &#123;
                <span class="hljs-built_in">setTimeout</span>(<span class="hljs-function">() =&gt;</span> &#123;
                    <span class="hljs-built_in">console</span>.log(<span class="hljs-string">&#x27;初级前端：&#x27;</span>, name);
                    resolve()
                &#125;, <span class="hljs-number">1000</span>)
            &#125;)
        &#125;)
        <span class="hljs-built_in">this</span>.hooks.arch.tapPromise(<span class="hljs-string">&#x27;intermediate&#x27;</span>, <span class="hljs-function">(<span class="hljs-params">name</span>) =&gt;</span> &#123;
            <span class="hljs-keyword">return</span> <span class="hljs-keyword">new</span> <span class="hljs-built_in">Promise</span>(<span class="hljs-function">(<span class="hljs-params">resolve</span>) =&gt;</span> &#123;
                <span class="hljs-built_in">setTimeout</span>(<span class="hljs-function">() =&gt;</span> &#123;
                    <span class="hljs-built_in">console</span>.log(<span class="hljs-string">&#x27;中级前端：&#x27;</span>, name);
                    resolve()
                &#125;, <span class="hljs-number">1000</span>)
            &#125;)
        &#125;)
        <span class="hljs-built_in">this</span>.hooks.arch.tapPromise(<span class="hljs-string">&#x27;senior&#x27;</span>, <span class="hljs-function">(<span class="hljs-params">name</span>) =&gt;</span> &#123;
            <span class="hljs-keyword">return</span> <span class="hljs-keyword">new</span> <span class="hljs-built_in">Promise</span>(<span class="hljs-function">(<span class="hljs-params">resolve</span>) =&gt;</span> &#123;
                <span class="hljs-built_in">setTimeout</span>(<span class="hljs-function">() =&gt;</span> &#123;
                    <span class="hljs-built_in">console</span>.log(<span class="hljs-string">&#x27;高级前端：&#x27;</span>, name);
                    resolve()
                &#125;, <span class="hljs-number">1000</span>)
            &#125;)
        &#125;)
    &#125;
    <span class="hljs-comment">// 执行监听函数</span>
    <span class="hljs-function"><span class="hljs-title">publish</span>(<span class="hljs-params"></span>)</span> &#123;
        <span class="hljs-built_in">this</span>.hooks.arch.promise(<span class="hljs-string">&#x27;yxfan&#x27;</span>)
            .then(<span class="hljs-function">() =&gt;</span> &#123;
                <span class="hljs-built_in">console</span>.log(<span class="hljs-string">&#x27;封神啦！&#x27;</span>);
            &#125;)
    &#125;
&#125;
<span class="hljs-keyword">const</span> hookPromise = <span class="hljs-keyword">new</span> HookPromise();
hookPromise.subscribe();
hookPromise.publish();</code></pre></div>

<h4 id="实现：-5"><a href="#实现：-5" class="headerlink" title="实现："></a>实现：</h4><div class="code-wrapper"><pre><code class="hljs javascript"><span class="hljs-class"><span class="hljs-keyword">class</span> <span class="hljs-title">AsyncSeriesHook</span> </span>&#123;
    <span class="hljs-function"><span class="hljs-title">constructor</span>(<span class="hljs-params"></span>)</span> &#123;
        <span class="hljs-built_in">this</span>.tasks = [];
    &#125;
    <span class="hljs-function"><span class="hljs-title">tapAsync</span>(<span class="hljs-params">name, task</span>)</span> &#123;
        <span class="hljs-built_in">this</span>.tasks.push(task);
    &#125;
    <span class="hljs-function"><span class="hljs-title">callAsync</span>(<span class="hljs-params">...args</span>)</span> &#123;
        <span class="hljs-keyword">const</span> endCb = args.pop();
        <span class="hljs-keyword">let</span> index = <span class="hljs-number">0</span>;
        <span class="hljs-comment">// 递归</span>
        <span class="hljs-keyword">let</span> next = <span class="hljs-function">() =&gt;</span> &#123;
            <span class="hljs-keyword">if</span> (index === <span class="hljs-built_in">this</span>.tasks.length) &#123;
                <span class="hljs-keyword">return</span> endCb();
            &#125;
            <span class="hljs-keyword">let</span> task = <span class="hljs-built_in">this</span>.tasks[index];
            index++;
            task(...args, next);
        &#125;
        next();
    &#125;

    <span class="hljs-function"><span class="hljs-title">tapPromise</span>(<span class="hljs-params">name, task</span>)</span> &#123;
        <span class="hljs-built_in">this</span>.tasks.push(task);
    &#125;
    <span class="hljs-function"><span class="hljs-title">promise</span>(<span class="hljs-params">...args</span>)</span> &#123;
        <span class="hljs-keyword">const</span> [first, ...others] = <span class="hljs-built_in">this</span>.tasks;
        <span class="hljs-keyword">return</span> others.reduce(<span class="hljs-function">(<span class="hljs-params">pre, next</span>) =&gt;</span> &#123;
            <span class="hljs-keyword">return</span> pre.then(<span class="hljs-function">() =&gt;</span> next(...args));
        &#125;, first(...args));
    &#125;
&#125;
<span class="hljs-built_in">module</span>.exports = AsyncSeriesHook;</code></pre></div>

<h3 id="AsyncSeriesWaterfallHook"><a href="#AsyncSeriesWaterfallHook" class="headerlink" title="AsyncSeriesWaterfallHook"></a>AsyncSeriesWaterfallHook</h3><p>AsyncSeriesWaterfallHook 异步串行瀑布钩子，像瀑布一样的传递参数</p>
<p>注意：cb方法接受两个参数，第一个参数是错误信息，为null时，数据流向下，不为null则表示出错了，后续事件都不执行，直接执行callAsync的回调。第二个参数就要传递的数据</p>
<div class="code-wrapper"><pre><code class="hljs javascript"><span class="hljs-keyword">const</span> &#123; AsyncSeriesWaterfallHook &#125; = <span class="hljs-built_in">require</span>(<span class="hljs-string">&#x27;../real-tapable&#x27;</span>);

<span class="hljs-class"><span class="hljs-keyword">class</span> <span class="hljs-title">Hook</span> </span>&#123;
    <span class="hljs-function"><span class="hljs-title">constructor</span>(<span class="hljs-params"></span>)</span> &#123;
        <span class="hljs-built_in">this</span>.hooks = &#123;
            <span class="hljs-attr">arch</span>: <span class="hljs-keyword">new</span> AsyncSeriesWaterfallHook([<span class="hljs-string">&#x27;arg1&#x27;</span>])
        &#125;
    &#125;
    <span class="hljs-comment">// 注册监听函数</span>
    <span class="hljs-function"><span class="hljs-title">subscribe</span>(<span class="hljs-params"></span>)</span> &#123;
        <span class="hljs-built_in">this</span>.hooks.arch.tapAsync(<span class="hljs-string">&#x27;primary&#x27;</span>, <span class="hljs-function">(<span class="hljs-params">name, cb</span>) =&gt;</span> &#123;
            <span class="hljs-built_in">setTimeout</span>(<span class="hljs-function">() =&gt;</span> &#123;
                <span class="hljs-built_in">console</span>.log(<span class="hljs-string">&#x27;初级前端：&#x27;</span>, name);
                cb(<span class="hljs-literal">null</span>, <span class="hljs-string">&#x27;cheney&#x27;</span>)
            &#125;, <span class="hljs-number">1000</span>)
        &#125;)
        <span class="hljs-built_in">this</span>.hooks.arch.tapAsync(<span class="hljs-string">&#x27;intermediate&#x27;</span>, <span class="hljs-function">(<span class="hljs-params">data, cb</span>) =&gt;</span> &#123;
            <span class="hljs-built_in">setTimeout</span>(<span class="hljs-function">() =&gt;</span> &#123;
                <span class="hljs-built_in">console</span>.log(<span class="hljs-string">&#x27;中级前端：&#x27;</span>, data);
                cb(<span class="hljs-string">&#x27;error&#x27;</span>, <span class="hljs-string">&#x27;xiaopa&#x27;</span>) <span class="hljs-comment">// 出错啦，senior事件不会执行</span>
            &#125;, <span class="hljs-number">1000</span>)
        &#125;)
        <span class="hljs-built_in">this</span>.hooks.arch.tapAsync(<span class="hljs-string">&#x27;senior&#x27;</span>, <span class="hljs-function">(<span class="hljs-params">data, cb</span>) =&gt;</span> &#123;
            <span class="hljs-built_in">setTimeout</span>(<span class="hljs-function">() =&gt;</span> &#123;
                <span class="hljs-built_in">console</span>.log(<span class="hljs-string">&#x27;高级前端：&#x27;</span>, data);
                cb(<span class="hljs-literal">null</span>)
            &#125;, <span class="hljs-number">1000</span>)
        &#125;)
    &#125;
    <span class="hljs-comment">// 执行监听函数</span>
    <span class="hljs-function"><span class="hljs-title">publish</span>(<span class="hljs-params"></span>)</span> &#123;
        <span class="hljs-built_in">this</span>.hooks.arch.callAsync(<span class="hljs-string">&#x27;yxfan&#x27;</span>, <span class="hljs-function">() =&gt;</span> &#123;
            <span class="hljs-built_in">console</span>.log(<span class="hljs-string">&#x27;封神啦!&#x27;</span>);
        &#125;)
    &#125;
&#125;
<span class="hljs-keyword">const</span> hook = <span class="hljs-keyword">new</span> Hook();
hook.subscribe();
hook.publish();</code></pre></div>

<p>依次间隔1秒打印：”初级前端： yxfan”，“中级前端： yxfan”，“封神啦!”</p>
<p>用<code>tapPromise</code>注册事件：</p>
<div class="code-wrapper"><pre><code class="hljs javascript"><span class="hljs-class"><span class="hljs-keyword">class</span> <span class="hljs-title">HookPromise</span> </span>&#123;
    <span class="hljs-function"><span class="hljs-title">constructor</span>(<span class="hljs-params"></span>)</span> &#123;
        <span class="hljs-built_in">this</span>.hooks = &#123;
            <span class="hljs-attr">arch</span>: <span class="hljs-keyword">new</span> AsyncSeriesWaterfallHook([<span class="hljs-string">&#x27;arg1&#x27;</span>])
        &#125;
    &#125;
    <span class="hljs-comment">// 注册监听函数</span>
    <span class="hljs-function"><span class="hljs-title">subscribe</span>(<span class="hljs-params"></span>)</span> &#123;
        <span class="hljs-built_in">this</span>.hooks.arch.tapPromise(<span class="hljs-string">&#x27;primary&#x27;</span>, <span class="hljs-function">(<span class="hljs-params">name</span>) =&gt;</span> &#123;
            <span class="hljs-keyword">return</span> <span class="hljs-keyword">new</span> <span class="hljs-built_in">Promise</span>(<span class="hljs-function">(<span class="hljs-params">resolve</span>) =&gt;</span> &#123;
                <span class="hljs-built_in">setTimeout</span>(<span class="hljs-function">() =&gt;</span> &#123;
                    <span class="hljs-built_in">console</span>.log(<span class="hljs-string">&#x27;初级前端：&#x27;</span>, name);
                    resolve(<span class="hljs-string">&#x27;cheney&#x27;</span>)
                &#125;, <span class="hljs-number">1000</span>)
            &#125;)
        &#125;)
        <span class="hljs-built_in">this</span>.hooks.arch.tapPromise(<span class="hljs-string">&#x27;intermediate&#x27;</span>, <span class="hljs-function">(<span class="hljs-params">data</span>) =&gt;</span> &#123;
            <span class="hljs-keyword">return</span> <span class="hljs-keyword">new</span> <span class="hljs-built_in">Promise</span>(<span class="hljs-function">(<span class="hljs-params">resolve, reject</span>) =&gt;</span> &#123;
                <span class="hljs-built_in">setTimeout</span>(<span class="hljs-function">() =&gt;</span> &#123;
                    <span class="hljs-built_in">console</span>.log(<span class="hljs-string">&#x27;中级前端：&#x27;</span>, data);
                    <span class="hljs-comment">// resolve(&#x27;xiaopa&#x27;)</span>
                  	reject()
                &#125;, <span class="hljs-number">1000</span>)
            &#125;)
        &#125;)
        <span class="hljs-built_in">this</span>.hooks.arch.tapPromise(<span class="hljs-string">&#x27;senior&#x27;</span>, <span class="hljs-function">(<span class="hljs-params">data</span>) =&gt;</span> &#123;
            <span class="hljs-keyword">return</span> <span class="hljs-keyword">new</span> <span class="hljs-built_in">Promise</span>(<span class="hljs-function">(<span class="hljs-params">resolve, reject</span>) =&gt;</span> &#123;
                <span class="hljs-built_in">setTimeout</span>(<span class="hljs-function">() =&gt;</span> &#123;
                    <span class="hljs-built_in">console</span>.log(<span class="hljs-string">&#x27;高级前端：&#x27;</span>, data);
                    resolve()
                &#125;, <span class="hljs-number">1000</span>)
            &#125;)
        &#125;)
    &#125;
    <span class="hljs-comment">// 执行监听函数</span>
    <span class="hljs-function"><span class="hljs-title">publish</span>(<span class="hljs-params"></span>)</span> &#123;
        <span class="hljs-built_in">this</span>.hooks.arch.promise(<span class="hljs-string">&#x27;yxfan&#x27;</span>)
            .then(<span class="hljs-function">() =&gt;</span> &#123;
                <span class="hljs-built_in">console</span>.log(<span class="hljs-string">&#x27;封神啦！&#x27;</span>);
            &#125;)
    &#125;
&#125;
<span class="hljs-keyword">const</span> hookPromise = <span class="hljs-keyword">new</span> HookPromise();
hookPromise.subscribe();
hookPromise.publish();</code></pre></div>

<h4 id="实现：-6"><a href="#实现：-6" class="headerlink" title="实现："></a>实现：</h4><div class="code-wrapper"><pre><code class="hljs javascript"><span class="hljs-class"><span class="hljs-keyword">class</span> <span class="hljs-title">AsyncSeriesWaterfallHook</span> </span>&#123;
    <span class="hljs-function"><span class="hljs-title">constructor</span>(<span class="hljs-params"></span>)</span> &#123;
        <span class="hljs-built_in">this</span>.tasks = [];
    &#125;
    <span class="hljs-function"><span class="hljs-title">tapAsync</span>(<span class="hljs-params">name, task</span>)</span> &#123;
        <span class="hljs-built_in">this</span>.tasks.push(task);
    &#125;
    <span class="hljs-function"><span class="hljs-title">callAsync</span>(<span class="hljs-params">...args</span>)</span> &#123;
        <span class="hljs-keyword">const</span> endCb = args.pop();
        <span class="hljs-keyword">let</span> index = <span class="hljs-number">0</span>;
        <span class="hljs-comment">// 递归</span>
        <span class="hljs-keyword">let</span> next = <span class="hljs-function">(<span class="hljs-params">err, data</span>) =&gt;</span> &#123;
            <span class="hljs-keyword">let</span> task = <span class="hljs-built_in">this</span>.tasks[index];
            <span class="hljs-keyword">if</span> (!task || err) <span class="hljs-keyword">return</span> endCb();
            <span class="hljs-keyword">if</span> (index == <span class="hljs-number">0</span>) &#123;
                task(...args, next);
            &#125; <span class="hljs-keyword">else</span> &#123;
                task(data, next);
            &#125;
            index++;
        &#125;
        next();
    &#125;
    <span class="hljs-function"><span class="hljs-title">tapPromise</span>(<span class="hljs-params">name, task</span>)</span> &#123;
        <span class="hljs-built_in">this</span>.tasks.push(task);
    &#125;
    <span class="hljs-function"><span class="hljs-title">promise</span>(<span class="hljs-params">...args</span>)</span> &#123;
        <span class="hljs-keyword">const</span> [first, ...others] = <span class="hljs-built_in">this</span>.tasks;
        <span class="hljs-keyword">return</span> others.reduce(<span class="hljs-function">(<span class="hljs-params">pre, next</span>) =&gt;</span> &#123;
            <span class="hljs-keyword">return</span> pre.then(<span class="hljs-function">(<span class="hljs-params">data</span>) =&gt;</span> next(data));
        &#125;, first(...args));
    &#125;
&#125;
<span class="hljs-built_in">module</span>.exports = AsyncSeriesWaterfallHook;</code></pre></div>

<p><a target="_blank" rel="noopener" href="https://gitee.com/fanxiaopa/tabaple">代码地址</a></p>

            </div>
            <hr>
            <div>
              <div class="post-metas mb-3">
                
                
                  <div class="post-meta">
                    <i class="iconfont icon-tags"></i>
                    
                      <a class="hover-with-bg" href="/tags/webpack/">webpack</a>
                    
                  </div>
                
              </div>
              
                <p class="note note-warning">转载请注明出处！</p>
              
              
                <div class="post-prevnext">
                  <article class="post-prev col-6">
                    
                    
                      <a href="/2021/12/11/%E3%80%8A%E6%B4%BB%E7%9D%80%E3%80%8B/">
                        <i class="iconfont icon-arrowleft"></i>
                        <span class="hidden-mobile">《活着》</span>
                        <span class="visible-mobile">上一篇</span>
                      </a>
                    
                  </article>
                  <article class="post-next col-6">
                    
                    
                      <a href="/2021/10/31/%E8%AF%BB%E3%80%8A%E6%88%90%E4%BA%8B%E5%BF%83%E6%B3%95%E3%80%8BPart1/">
                        <span class="hidden-mobile">读《成事心法》Part1</span>
                        <span class="visible-mobile">下一篇</span>
                        <i class="iconfont icon-arrowright"></i>
                      </a>
                    
                  </article>
                </div>
              
            </div>

            
          </article>
        </div>
      </div>
    </div>
    
      <div class="d-none d-lg-block col-lg-2 toc-container" id="toc-ctn">
        <div id="toc">
  <p class="toc-header"><i class="iconfont icon-list"></i>&nbsp;目录</p>
  <div class="toc-body" id="toc-body"></div>
</div>

      </div>
    
  </div>
</div>

<!-- Custom -->


    

    
      <a id="scroll-top-button" href="#" role="button">
        <i class="iconfont icon-arrowup" aria-hidden="true"></i>
      </a>
    

    
      <div class="modal fade" id="modalSearch" tabindex="-1" role="dialog" aria-labelledby="ModalLabel"
     aria-hidden="true">
  <div class="modal-dialog modal-dialog-scrollable modal-lg" role="document">
    <div class="modal-content">
      <div class="modal-header text-center">
        <h4 class="modal-title w-100 font-weight-bold">搜索</h4>
        <button type="button" id="local-search-close" class="close" data-dismiss="modal" aria-label="Close">
          <span aria-hidden="true">&times;</span>
        </button>
      </div>
      <div class="modal-body mx-3">
        <div class="md-form mb-5">
          <input type="text" id="local-search-input" class="form-control validate">
          <label data-error="x" data-success="v"
                 for="local-search-input">关键词</label>
        </div>
        <div class="list-group" id="local-search-result"></div>
      </div>
    </div>
  </div>
</div>
    

    
  </main>

  <footer class="text-center mt-5 py-3">
  <div class="footer-content">
    
  </div>
  

  

  
</footer>

<!-- SCRIPTS -->

  <script  src="https://fastly.jsdelivr.net/npm/nprogress@0.2.0/nprogress.min.js" ></script>
  <link  rel="stylesheet" href="https://fastly.jsdelivr.net/npm/nprogress@0.2.0/nprogress.min.css" />

  <script>
    NProgress.configure({"showSpinner":false,"trickleSpeed":100})
    NProgress.start()
    window.addEventListener('load', function() {
      NProgress.done();
    })
  </script>


<script  src="https://fastly.jsdelivr.net/npm/jquery@3.5.1/dist/jquery.min.js" ></script>
<script  src="https://fastly.jsdelivr.net/npm/bootstrap@4.5.3/dist/js/bootstrap.min.js" ></script>
<script  src="/js/debouncer.js" ></script>
<script  src="/js/events.js" ></script>
<script  src="/js/plugins.js" ></script>

<!-- Plugins -->


  
    <script  src="/js/lazyload.js" ></script>
  



  



  <script  src="https://fastly.jsdelivr.net/npm/tocbot@4.12.0/dist/tocbot.min.js" ></script>



  <script  src="https://fastly.jsdelivr.net/npm/@fancyapps/fancybox@3.5.7/dist/jquery.fancybox.min.js" ></script>



  <script  src="https://fastly.jsdelivr.net/npm/anchor-js@4.3.0/anchor.min.js" ></script>



  <script defer src="https://fastly.jsdelivr.net/npm/clipboard@2.0.6/dist/clipboard.min.js" ></script>



  <script defer src="https://busuanzi.ibruce.info/busuanzi/2.3/busuanzi.pure.mini.js" ></script>


  <script defer src="/js/leancloud.js" ></script>



  <script  src="https://fastly.jsdelivr.net/npm/typed.js@2.0.11/lib/typed.min.js" ></script>
  <script>
    (function (window, document) {
      var typing = Fluid.plugins.typing;
      var title = document.getElementById('subtitle').title;
      
      typing(title)
      
    })(window, document);
  </script>



  <script  src="/js/local-search.js" ></script>
  <script>
    (function () {
      var path = "/local-search.xml";
      var inputArea = document.querySelector("#local-search-input");
      inputArea.onclick = function () {
        searchFunc(path, 'local-search-input', 'local-search-result');
        this.onclick = null
      }
    })()
  </script>












  

  

  

  

  

  





<!-- 主题的启动项 保持在最底部 -->
<script  src="/js/boot.js" ></script>



</body>
</html>
